<template>
  <a-modal
    :title="model && model.userId > 0 ? '编辑' : '新增'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout" >
        <a-form-item v-show="false" label="主键ID">
          <a-input v-decorator="['userId', { initialValue: 0 }]" disabled />
        </a-form-item>
        <a-form-item label="用户">
          <a-input
            placeholder="请输入用户"
            v-decorator="['nickName', {rules: [{required: true, message: '请输入用户姓名'}]}]" />
        </a-form-item>
        <a-form-item label="手机号">
          <a-input
            placeholder="请输入手机号"
            v-decorator="['phonenumber', {rules: [{required: true, message: '请输入手机号' },{validator: mobilephoneValidate}]}]" />
        </a-form-item>
        <a-form-item label="邮箱">
          <a-input
            placeholder="请输入邮箱"
            v-decorator="['email', {rules: [{required: false, message: '请输入邮箱'}]}]" />
        </a-form-item>
        <a-form-item label="所属角色">
          <a-select
            placeholder="请选择所属角色"
            v-decorator="[
              'roleId',
              {rules: [{ required: true, message: '请选择所属角色'}]}
            ]" >
            <a-select-option v-for="item in roleList" :key="item.roleId" :value="item.roleId">{{ item.roleName }}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'

// 表单字段
const fields = ['userId', 'nickName', 'phonenumber', 'email', 'roleId']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    roleList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    testMobilephone (str) {
      const regex = /^1[3456789]\d{9}$/
      if (!regex.test(str)) {
        return false
      } else {
        return true
      }
    },
    mobilephoneValidate (rule, value, callback) {
      // 主要就是添加一个对undefined和空串的判断
      if (typeof (value) === 'undefined' || value === '') {
        callback()
      } else {
        if (!this.testMobilephone(value)) {
          callback(new Error('请输入正确手机格式'))
        } else {
          callback()
        }
      }
    }
  }
}
</script>
